package edu.api.date_api;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

public class Demo2 {

    public static void main(String[] args) {
        test3();

    }

    /**
     * 日期格式化
     *
     */
    public static void test3(){
        String time = "2022-12-13";
//        LocalDate localDate = LocalDate.parse(time, DateTimeFormatter.ISO_DATE);
        //字符串日期转LocalDate
        LocalDate localDate = LocalDate.parse(time, DateTimeFormatter.ofPattern("yyyy-M-dd"));
        System.out.println("localDate = " + localDate);
        //LocalDate转字符串
        String s = localDate.format(DateTimeFormatter.ofPattern("yy-M-dd"));
        System.out.println("s = " + s);

    }




    /**
     * LocalDate LocalTime  LocalDateTime 操作：
     */
    public static void test2(){
        LocalDate localDate = LocalDate.now();
        //获取日期中相关字段信息
        int year = localDate.getYear();
        System.out.println("year = " + year);
        Month month = localDate.getMonth();
        System.out.println("month = " + month);
        int day = localDate.get(ChronoField.DAY_OF_MONTH);
        System.out.println("day = " + day);
        int day_of_week = localDate.get(ChronoField.DAY_OF_WEEK);
        System.out.println("day_of_week = " + day_of_week);
        //修改年份
        LocalDate localDate1 = localDate.withYear(2021);
        System.out.println("localDate1 = " + localDate1);
        LocalDate localDate2 = localDate1.withMonth(10);
        System.out.println("localDate2 = " + localDate2);
        int lengthOfMonth = localDate2.lengthOfMonth();
        System.out.println("lengthOfMonth = " + lengthOfMonth);
        int lengthOfYear = localDate2.lengthOfYear();
        System.out.println("lengthOfYear = " + lengthOfYear);
        //将日期day + 100  多少天之后的日期
        LocalDate localDate3 = localDate2.plusDays(100);
        System.out.println("localDate3 = " + localDate3);
        //多少天之前的日期
        LocalDate localDate4 = localDate3.minusDays(200);
        System.out.println("localDate4 = " + localDate4);
        //计算两个日期相差多少天
        long until = localDate3.until(localDate4, ChronoUnit.DAYS);
        System.out.println("until = " + until);

    }


    /**
     * LocalDate :本地日期
     * LocalTime ：本地时间
     * LocalDateTime ：本地时间+日期
     */
    public static void test1(){
        //获取当前时间
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("dateTime = " + dateTime);
        //指定获取的日期 + 时间
        LocalDateTime localDateTime = LocalDateTime.of(2020, 9, 1, 10, 00, 21);
        System.out.println("localDateTime = " + localDateTime);

    }


}
